﻿<UserControl x:Class="BloggerAutoPoster.Shell.Views.PostsList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:extToolkit="clr-namespace:Microsoft.Windows.Controls"
             xmlns:Behaviors="clr-namespace:BloggerAutoPoster.Common.Behaviors;assembly=BloggerAutoPoster.Common"
             xmlns:Ctrls="clr-namespace:BloggerAutoPoster.Shell.Controls"
             xmlns:vm="clr-namespace:BloggerAutoPoster.Infrastructure.ViewModels;assembly=BloggerAutoPoster.Infrastructure"
             xmlns:views="clr-namespace:BloggerAutoPoster.Shell.Views"
             xmlns:Converters="clr-namespace:BloggerAutoPoster.Common.Converters;assembly=BloggerAutoPoster.Common"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="600">
    <UserControl.Resources>
        <Converters:WaterMarkConverter x:Key="convWaterMarkConverter" />
        <Converters:RunDirectionConverter x:Key="convRunDirectionConverter" />
        <Converters:CurrentPageConverter x:Key="convCurrentPageConverter" />
        <vm:PostsListViewModel x:Key="vmPostsListViewModel" />

        <ControlTemplate TargetType="{x:Type GroupItem}" x:Key="myGroupItemTemplate">
            <Expander IsExpanded="True">
                <Expander.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
                        <TextBlock FontWeight="Bold" Text=" (" />
                        <TextBlock FontWeight="Bold" Text="{Binding ItemCount}"/>
                        <TextBlock FontWeight="Bold" Text=")" />
                    </StackPanel>
                </Expander.Header>
                <Expander.Content>
                    <ItemsPresenter />
                </Expander.Content>
            </Expander>
        </ControlTemplate>
    </UserControl.Resources>
    <TabControl Margin="7" Grid.Row="0" Name="tabControl1" 
				DataContext="{Binding Source={StaticResource vmPostsListViewModel}}"
				TabStripPlacement="Left">
        <TabItem>
            <TabItem.Header>
                <StackPanel VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Center" Margin="1">
                    <Image Source="Images/blogger.png" Margin="1" VerticalAlignment="Center" Width="16" Height="16" HorizontalAlignment="Center" />
                    <TextBlock Text="BAP" Margin="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
                </StackPanel>
            </TabItem.Header>
            <extToolkit:BusyIndicator IsBusy="{Binding PostsListGuiData.IsBusy}"                                                              
							  BusyContent="Please wait..." 
							  DisplayAfter="00:00:00">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <ToolBar Grid.Row="0" Margin="7">
                        <Button x:Name="btnStart" IsEnabled="{Binding PostsListGuiData.IsEnabled}"
								Command="{Binding DoStart}" ToolTip="Download News">
                            <Image Margin="1" Width="16" Height="16" Source="Images/download.png"  />
                        </Button>
                        <Separator />
                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
							Command="{Binding DoSelectUnsentNews}" ToolTip="Select Unsent News Items">
                            <Image Margin="1" Source="Images/newspaper.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>

                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
							Command="{Binding DoPostAll}" ToolTip="Post Selected News To Blogger">
                            <Image Margin="1" Source="Images/blogger.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>

                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
							Command="{Binding DoMarkAllNewsAsSent}" ToolTip="Mark All News As Sent">
                            <Image Margin="1" Source="Images/newspaper_delete.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>
                        <Separator />
                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
								Command="{Binding DoSave}" ToolTip="Export Selected News">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <Behaviors:SaveFileDialogBoxBehavior
										DialogFilter="Html files (*.html)|*.html"                                        
										FileDialogDialogResultCommand="{Binding PostsListGuiData.SaveFilePath, Mode=TwoWay}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                            <Image Margin="1" Source="Images/export16.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>
                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
							Command="{Binding DoCopyAll}" ToolTip="Copy Selected News To Clipboard">
                            <Image Margin="1" Source="Images/CopyHS.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>
                        <Separator />
                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
							Command="{Binding DoSaveChanges}" ToolTip="Save Changes">
                            <Image Margin="1" Source="Images/save.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>

                        <Button IsEnabled="{Binding PostsListGuiData.IsEnabled}"
							Command="{Binding DoDelete}" ToolTip="Delete Selected News">
                            <Image Margin="1" Source="Images/delete.png" Width="16" Height="16" Stretch="UniformToFill" />
                        </Button>
                        <Separator />
                        <Button Command="{Binding DoCfg}" ToolTip="Config">
                            <StackPanel HorizontalAlignment="Center" Margin="1">
                                <Image Source="Images/config.png" Width="16" Height="16" Stretch="UniformToFill" />
                                <Ctrls:BetterPopup VerticalAlignment="Top" AllowsTransparency="True" 
									   StaysOpen="True"
									   IsOpen="{Binding PostsListGuiData.PopupDoCfgIsOpen}"
									   IsTopmost="False"
									   PopupAnimation="Slide" Margin="5">
                                    <Border Background="Gray" 
											BorderThickness="2" CornerRadius="6" BorderBrush="Black">
                                        <views:ConfigUc />
                                    </Border>
                                </Ctrls:BetterPopup>
                            </StackPanel>
                        </Button>
                    </ToolBar>

                    <GroupBox Margin="7,1,7,7" Grid.Row="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <StackPanel Orientation="Horizontal" Grid.Row="0" Margin="2">
                                <StackPanel.Visibility>
                                    <MultiBinding Converter="{StaticResource convWaterMarkConverter}">
                                        <Binding ElementName="txtSearch" Path="Text.IsEmpty" />
                                        <Binding ElementName="txtSearch" Path="IsFocused" />
                                    </MultiBinding>
                                </StackPanel.Visibility>
                                <Image Source="Images/toolbar_find.png"                    
									   Margin="3,2,2,2" Width="16" Height="16" Stretch="UniformToFill" VerticalAlignment="Center" />
                                <TextBlock Margin="2" Text="Search"                        
										   Foreground="DarkGray" VerticalAlignment="Center" />
                            </StackPanel>
                            <TextBox Grid.Row="0" Text="{Binding PostsListGuiData.SearchText, 
												                 Mode=TwoWay, UpdateSourceTrigger=Explicit}"
                                     Behaviors:InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text"
                                     FlowDirection="{Binding Path=PostsListGuiData.SearchText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource convRunDirectionConverter}}"
									 Margin="2" Name="txtSearch" Background="Transparent"/>

                            <ListView Grid.Row="1" Margin="2"
                                      Behaviors:AutoScrollListView.AutoScroll="True"
                                      Behaviors:ScrollToIndexBehavior.ScrollToIndex="{Binding PostsListGuiData.ScrollToIndex, Mode=TwoWay}"
									  SelectedItem="{Binding PostsListGuiData.SelectedItem, Mode=TwoWay}"
                                      VirtualizingStackPanel.IsVirtualizing="True"
                                      VirtualizingStackPanel.VirtualizationMode="Recycling"
									  ItemsSource="{Binding NewsItemsDataView}">
                                <ListView.GroupStyle>
                                    <GroupStyle>
                                        <GroupStyle.ContainerStyle>
                                            <Style TargetType="{x:Type GroupItem}">
                                                <Setter Property="Template" Value="{StaticResource myGroupItemTemplate}" />
                                            </Style>
                                        </GroupStyle.ContainerStyle>
                                    </GroupStyle>
                                </ListView.GroupStyle>
                                <ListView.View>
                                    <GridView>
                                        <GridViewColumn Width="Auto">
                                            <GridViewColumn.Header>
                                                <CheckBox x:Name="chkSelectAll" Command="{Binding DoSelectAll}" CommandParameter="{Binding ElementName=chkSelectAll, Path=IsChecked}" />
                                            </GridViewColumn.Header>
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <CheckBox Margin="1" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn Width="500" Header="News">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <StackPanel>
                                                        <TextBlock Foreground="Cyan" HorizontalAlignment="Center"
															   Width="Auto" Margin="1"
															   Behaviors:WebHyperlink.LaunchDefaultBrowser="True" 
															   VerticalAlignment="Center"><Hyperlink Foreground="Cyan" 
                                                                                                     FlowDirection="{Binding Path=Title, Mode=TwoWay, Converter={StaticResource convRunDirectionConverter}}"
                                                                                                     NavigateUri="{Binding Link, Mode=TwoWay}"><TextBlock Width="Auto" Foreground="Cyan" Text="{Binding Title}"/></Hyperlink></TextBlock>
                                                        <TextBox Text="{Binding Path=Link, Mode=TwoWay}" Margin="1"/>
                                                        <TextBox FlowDirection="{Binding Path=Title, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource convRunDirectionConverter}}"
                                                                 Text="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="1"/>
                                                    </StackPanel>
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn Width="300" Header="Annotation">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBox Margin="1"  TextAlignment="Center" 
                                                               FlowDirection="{Binding Path=Annotation, Mode=TwoWay, Converter={StaticResource convRunDirectionConverter}}"  
                                                               Text="{Binding Annotation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                        <GridViewColumn Width="150" Header="AddDate">
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Margin="1" TextAlignment="Center" Text="{Binding AddDate}" />
                                                </DataTemplate>
                                            </GridViewColumn.CellTemplate>
                                        </GridViewColumn>
                                    </GridView>
                                </ListView.View>
                                <ListView.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Command="{Binding DoOpenThisFile}" Header="Open">
                                            <MenuItem.Icon>
                                                <Image Stretch="UniformToFill" Width="16" Height="16" 
													   Source="images/internet_explorer.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Command="{Binding DoCopyThisUrlToClipboard}" Header="Copy Url To Clipboard">
                                            <MenuItem.Icon>
                                                <Image Stretch="UniformToFill" Width="16" Height="16" 
													   Source="images/CopyHS.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </ContextMenu>
                                </ListView.ContextMenu>
                            </ListView>

                            <StackPanel Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                <Button Height="23" Margin="2" Width="40" ToolTip="First" Command="{Binding GoFirst}">
                                    <Image Stretch="Fill" Width="12" Height="12" 
										   Source="images/stop_left.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Button>
                                <Button Height="23" Margin="2" Width="40" ToolTip="Previous" Command="{Binding GoPrevious}">
                                    <Image Stretch="UniformToFill" Width="16" Height="16" 
										   Source="images/left_arrow.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Button>
                                <TextBlock Margin="2" VerticalAlignment="Center"  Text="{Binding ListViewGuiData.CurrentPage, Converter={StaticResource convCurrentPageConverter}}"  />
                                <TextBlock Margin="2" VerticalAlignment="Center" Text="/" />
                                <TextBlock Margin="2" VerticalAlignment="Center" Text="{Binding ListViewGuiData.TotalPage}" />

                                <Button Height="23" Margin="2" Width="40" ToolTip="Next" Command="{Binding GoNext}">
                                    <Image Stretch="UniformToFill" Width="16" Height="16" 
										   Source="images/arrow_right.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Button>
                                <Button Height="23" Margin="2" Width="40" ToolTip="Last" Command="{Binding GoLast}">
                                    <Image Stretch="Fill" Width="12" Height="12" 
										   Source="images/stop_right.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Button>
                            </StackPanel>
                        </Grid>
                    </GroupBox>
                </Grid>
            </extToolkit:BusyIndicator>
        </TabItem>
        <TabItem>
            <TabItem.Header>
                <StackPanel VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Center" Margin="1">
                    <Image Source="Images/event16.png" Margin="1" VerticalAlignment="Center" Width="16" Height="16" HorizontalAlignment="Center" />
                    <TextBlock Text="About" Margin="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
                </StackPanel>
            </TabItem.Header>
            <views:About />
        </TabItem>
    </TabControl>
</UserControl>